//
//  LogInViewControll.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/29.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class LogInViewControll: MainViewController {
    
    var logInView:LogInView!//界面
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.addLogInView()
        self.addClickEventToView()
        self.addNotifiction()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        self.operationWithNaviAndTabBar()
    }
}


//MARK:--UI设置
extension LogInViewControll{
    //对navigation 和tabbar的操作
    func operationWithNaviAndTabBar(){
        self.navigationController?.navigationBarHidden = true
        self.hidesBottomBarWhenPushed = true
        self.tabBarController?.tabBar.hidden = true
    }
    
    //将界面添加进来
    func addLogInView(){
        logInView = LogInView(frame: MainBounds)
        self.view.addSubview(logInView)
    }
    
    //给loginView的控件添加点击事件
    func addClickEventToView(){
        //给四个登录方式添加点击事件
        for i in 0..<4{
            let logWayButton = self.view.viewWithTag(100860+i) as! UIButton
            logWayButton.addTarget(self, action: #selector(logWayButtonClick(_:)), forControlEvents: .TouchUpInside)
        }
        
        //给注册账号添加点击事件
        logInView.registerButton.addTarget(self, action: #selector(self.registerButtonClick(_:)), forControlEvents: .TouchUpInside)
        
        //游客访问 添加点击事件
        logInView.visitorButton.addTarget(self, action: #selector(self.visitorButtonClick(_:)), forControlEvents: .TouchUpInside)
    }
    
}

//MARK:--点击事件
extension LogInViewControll{
    //登录方式 点击事件
    func logWayButtonClick(sender:UIButton){
        let tag = sender.tag
        switch tag {
        case 100860:
            self.navigationController?.pushViewController(PhoneLogInViewController(), animated: true)
        case 100861:
            print("qq")
        case 100862:
            print("微信")
        case 100863:
            print("微博")
        default:
            break
        }
    }
    
    //注册账户 点击事件
    func registerButtonClick(sender:UIButton){
        self.navigationController?.pushViewController(RegisterViewController(), animated: true)
    }
    
    //注册账户 点击事件
    func visitorButtonClick(sender:UIButton){
        print("点击了游客")
    }
}

//MARK:--notifaction
extension LogInViewControll{
   //添加通知
    func addNotifiction(){
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.logInNotifiction(_:)), name: Notifiction.LOGIN, object: nil)
    }
    
    //登录的通知事件
    func logInNotifiction(sender:NSNotification){
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: ISLOGIN)
        self.removeFromParentViewController()
        self.view.removeFromSuperview()
    }
    
}











